<?php

/**
 * This is the model class for table "items".
 *
 * The followings are the available columns in table 'items':
 * @property string $id
 * @property string $title
 * @property string $descrip
 * @property string $link
 * @property string $pubDate
 * @property string $fuente
 * @property string $categoria
 * @property string $fecha
 */
class Items extends CActiveRecord
{        
        public $rtadelp='';
    
	/**
	 * Returns the static model of the specified AR class.
	 * @return Items the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
        
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'items';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title', 'length', 'max'=>250),
			array('descrip', 'length', 'max'=>2500),
			array('link', 'length', 'max'=>200),
			array('pubDate', 'length', 'max'=>50),
			array('fuente, categoria', 'length', 'max'=>45),
			array('fecha', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, descrip, link, pubDate, fuente, categoria, fecha', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
			'descrip' => 'Descrip',
			'link' => 'Link',
			'pubDate' => 'Pub Date',
			'fuente' => 'Fuente',
			'categoria' => 'Categoria',
			'fecha' => 'Fecha',
                        'rtadelp' => 'Respuesta DeLP',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('descrip',$this->descrip,true);
		$criteria->compare('link',$this->link,true);
		$criteria->compare('pubDate',$this->pubDate,true);
		$criteria->compare('fuente',$this->fuente,true);
		$criteria->compare('categoria',$this->categoria,true);
		$criteria->compare('fecha',$this->fecha,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
        
        public function findByPk($pk,$condition='',$params=array())
        {
            Yii::trace(get_class($this).'.findByPk()','system.db.ar.CActiveRecord');
            $prefix=$this->getTableAlias(true).'.';
            $criteria=$this->getCommandBuilder()->createPkCriteria($this->getTableSchema(),$pk,$condition,$params,$prefix);
            $q_result=$this->query($criteria);
            if ($q_result != null) {
                $DeLP = new DeLP();
//                $DeLP->connect();
                $user = Yii::app()->user->getName();
                $q_result->rtadelp = $DeLP->callDeLP($user,$pk);//TODO: Cambiar al user logueado
            }
            return $q_result;
        }
        
        public function getRtaDeLP($pk){
            
            $DeLP = new DeLP();            
            /*$DeLP->connect();
            $DeLP->addTrustSources();
            $DeLP->addTrustUsers();
            $DeLP->addReportsSource();
            $DeLP->addTrustReports();
            
            Yii::log($DeLP->delp_code, 'error');
            */
            $user = Yii::app()->user->getName();
                       
            return  $this->rtadelp = $DeLP->callDeLP($user,$pk);
        }     
        
}